<template :model="SortingConfig">
  <a-drawer :title="SortingConfig.Name+'--叶片分拣顺序'" width="70%" :bordered="false" placement="right" :closable="false" :visible="visible" @close="onClose">
    <div class="table-operator">
      <a-button type="primary" v-action:Add icon="plus" @click="handleAdd">添加</a-button>
      <a-button type="primary" v-action:Delete icon="delete" @click="handleDelete()">批量删除</a-button>
    </div>

    <s-table ref="table" size="default" rowKey="Id" :columns="columns" :data="loadData" :rowSelection="rowSelection" showPagination="auto">
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:Update @click="handleUp(record)">上移</a>
          <a-divider v-action:Update type="vertical" />
          <a v-action:Update @click="handleDown(record)">下移</a>
          <a-divider v-action:Delete type="vertical" />
          <a v-action:Delete @click="handleDelete([record])">删除</a>
        </template>
      </span>
    </s-table>
    <EditForm ref="editForm" @Success="()=>{this.$refs.table.refresh()}"></EditForm>
    <LocationForm ref="locationForm" @Success="(location)=>{this.handleSaveData(location)}"></LocationForm>
  </a-drawer>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import MainSvc from '@/api/Define/MaterielSortingSequenceSvc'
import ConfigSvc from '@/api/Define/MaterielSortingConfigSvc'
import EditForm from './Edit'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'
import LocationForm from '../MaterielSortingLocation/Choose'

const columns = [
  { title: '顺序号', dataIndex: 'OrderNo', sorter: true },
  { title: '抓料料位', dataIndex: 'SortingLocation.Location.Code', sorter: true },
  { title: '料位物料', dataIndex: 'SortingLocation.Spec.Code', sorter: true },
  { title: '操作', dataIndex: 'action', width: '200px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    ConfigSvc,
    EnumSelect,
    EnumName,
    EditForm,
    LocationForm
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: { SortingConfigId: '', Keyword: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var _query = Object.assign({}, { ...this.queryParam })
        for (const key in _query) {
          if (moment.isMoment(_query[key])) {
            _query[key] = _query[key].format('YYYY-MM-DD')
          }
        }
        const requestParameters = Object.assign({ sortField: 'OrderNo', sortOrder: 'ASC', Search: _query }, parameter)
        console.log('loadData request parameters:', requestParameters)
        return MainSvc.GetPage(requestParameters)
      },
      selectedRowKeys: [],
      selectedRows: [],
      SortingConfig: { Id: '' }
    }
  },
  filters: {
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    openDrawer(record) {
        this.visible = true
        this.SortingConfig = record
        this.resetSearchForm()
        this.$refs.table.refresh()
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
      this.$refs.locationForm.openForm(this.SortingConfig, '新增')
    },
    handleSaveData(location) {
      this.loading = true
        MainSvc.Save({ Id: null, SortingConfigId: this.SortingConfig.Id, LocationId: location.Id, OrderNo: 1, Quantity: 1 }).then(result => {
          this.loading = false
          if (result.Success) {
            ConfigSvc.ModifyGrabCount(this.SortingConfig.Id).then(result => {})
            this.$message.success(result.Msg)
            this.resetSearchForm()
            this.$refs.table.refresh()
          } else {
            this.$message.error(result.Msg)
          }
        })
    },
    handleUp(record) {
      if (record.OrderNo > 1) {
        MainSvc.Up(record.Id).then(result => {
              this.$refs.table.refresh()
            })
      }
    },
    handleDown(record) {
      MainSvc.Down(record.Id).then(result => {
              this.$refs.table.refresh()
            })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = { SortingConfigId: this.SortingConfig.Id, Keyword: '' }
    },
    onClose() {
        this.visible = false
    },
    handleDelete(rows) {
      if (!rows) rows = this.selectedRows
      var thisObj = this
      var ids = rows.map(value => value.Id)
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            MainSvc.Delete(ids).then(result => {
              resolve()
              if (result.Success) {
                ConfigSvc.ModifyGrabCount(this.SortingConfig.Id).then(result => {})
                thisObj.$message.success('操作成功!')
                thisObj.$refs.table.refresh()
              } else {
                thisObj.$message.error(result.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
